﻿

namespace Oddo.Concept.Crm.Client
{
    using System.Windows;
    using Microsoft.Practices.ServiceLocation;
    using Oddo.Concept.Crm.Common.Diagnostic;
    using Oddo.Concept.Crm.Common.Presentation;
    using System.Threading;
    using System.Globalization;

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            this.ShutdownMode = ShutdownMode.OnMainWindowClose;
            this.DispatcherUnhandledException += OnDispatcherUnhandledException;

            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr-FR");

            var bootstrapper = new ApplicationBootStrapper();
            bootstrapper.Run();
        }

        private void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            IErrorReportManager errorReportingManager = null;
            try
            {
                errorReportingManager = ServiceLocator.Current.GetInstance<IErrorReportManager>();
            }
            catch
            {
                return;
            }
            
            var report = errorReportingManager.CreateReportFromException(e.Exception);
            if (report == null || !errorReportingManager.ShouldSendReport) return;
            try
            {
                report.Build();
                errorReportingManager.SendErrorReport(report);
                e.Handled = true;
            }
            catch
            {
                return;
            }
        }

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
        }
    }
}
